//
//  UIButton+PopAnimation.m
//  RecordApp
//
//  Created by TuanNT on 5/11/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "UIButton+PopAnimation.h"

@implementation UIButton (PopAnimation)

- (void)popOutsideWithDuration:(NSTimeInterval)duration {
	[self popOutsideWithDuration:duration completionBlock:nil];
}

- (void)popInsideWithDuration:(NSTimeInterval)duration {
	[self popInsideWithDuration:duration completionBlock:nil];
}

- (void)popOutsideWithDuration:(NSTimeInterval)duration completionBlock:(void (^)(BOOL finished))block {
	__weak typeof(self) weakSelf = self;

	self.transform = CGAffineTransformIdentity;
	[UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
	    [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
	        typeof(self) strongSelf = weakSelf;
	        strongSelf.transform = CGAffineTransformMakeScale(1.5, 1.5);
		}];
	    [UIView addKeyframeWithRelativeStartTime:1 / 3.0 relativeDuration:1 / 3.0 animations: ^{
	        typeof(self) strongSelf = weakSelf;
	        strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
		}];
	    [UIView addKeyframeWithRelativeStartTime:2 / 3.0 relativeDuration:1 / 3.0 animations: ^{
	        typeof(self) strongSelf = weakSelf;
	        strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
		}];
	} completion: ^(BOOL finished) {
	    if (block) {
	        block(finished);
		}
	}];
}

- (void)popInsideWithDuration:(NSTimeInterval)duration completionBlock:(void (^)(BOOL finished))block {
	__weak typeof(self) weakSelf = self;

	self.transform = CGAffineTransformIdentity;
	[UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
	    [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
	        typeof(self) strongSelf = weakSelf;
	        strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
		}];
	    [UIView addKeyframeWithRelativeStartTime:1 / 2.0 relativeDuration:1 / 2.0 animations: ^{
	        typeof(self) strongSelf = weakSelf;
	        strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
		}];
	} completion: ^(BOOL finished) {
	    if (block) {
	        block(finished);
		}
	}];
}

@end
